package com.wdbyte.springsqlite.initdata;


import com.wdbyte.springsqlite.model.User;
import com.wdbyte.springsqlite.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class DataInitializer implements CommandLineRunner {

    @Autowired
    private UserRepository userRepository;

//    @Override
//    public void run(String... args) throws Exception {
//        // 初始化数据
//        User user1 = new User();
//        user1.setName("Alice");
//        user1.setEmail("alice@example.com");
//
//        User user2 = new User();
//        user2.setName("Bob");
//        user2.setEmail("bob@example.com");
//
//        userRepository.save(user1);
//        userRepository.save(user2);
//    }

    @Override
    public void run(String... args) {
        if (userRepository.count() == 0) {
            User user = new User();
            user.setName("John Doe");
            user.setEmail("john.doe@example.com");
            userRepository.save(user);
            System.out.println("Inserted user: " + user.getName());
        } else {
            System.out.println("User already exists.");
        }
    }

}
